﻿using SD.Toolkits.Mapper;
using SlamDunk.MES.Domain.Entities.PlanContxt;
using SlamDunk.MES.IAppService.DTOs.Outputs.DefinitionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.FoundationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.AppService.Maps
{
    /// <summary>
    /// 计划相关映射
    /// </summary>
    public static class PlanMap
    {
        #region # 销售订单映射 —— static SaleOrderInfo ToDTO(this SaleOrder...
        /// <summary>
        /// 销售订单映射
        /// </summary>
        public static SaleOrderInfo ToDTO(this SaleOrder saleOrder, IDictionary<Guid, EnterpriseInfo> customerInfos)
        {
            SaleOrderInfo orderInfo = saleOrder.Map<SaleOrder, SaleOrderInfo>();
            orderInfo.CustomerInfo = customerInfos?[saleOrder.CustomerId];

            return orderInfo;
        }
        #endregion

        #region # 销售订单明细映射 —— static SaleOrderDetailInfo ToDTO(this SaleOrderDetail...
        /// <summary>
        /// 销售订单明细映射
        /// </summary>
        public static SaleOrderDetailInfo ToDTO(this SaleOrderDetail detail, IDictionary<Guid, MaterialInfo> productInfos)
        {
            SaleOrderDetailInfo detailInfo = detail.Map<SaleOrderDetail, SaleOrderDetailInfo>();
            detailInfo.ProductInfo = productInfos?[detail.ProductId];

            return detailInfo;
        }
        #endregion

        #region # 生产计划映射 —— static ProductionPlanInfo ToDTO(this ProductionPlan...
        /// <summary>
        /// 生产计划映射
        /// </summary>
        public static ProductionPlanInfo ToDTO(this ProductionPlan productionPlan, IDictionary<Guid, MaterialInfo> productInfos, IDictionary<Guid, BomInfo> bomInfos, IDictionary<Guid, SaleOrderInfo> saleOrderInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            ProductionPlanInfo planInfo = productionPlan.Map<ProductionPlan, ProductionPlanInfo>();
            planInfo.ProductInfo = productInfos?[productionPlan.ProductId];
            planInfo.BomInfo = bomInfos?[productionPlan.BomId];
            planInfo.SaleOrderInfo = productionPlan.SaleOrderId.HasValue
                ? saleOrderInfos?[productionPlan.SaleOrderId.Value]
                : null;
            planInfo.FacilityInfo = productionPlan.FacilityId.HasValue
                ? facilityInfos?[productionPlan.FacilityId.Value]
                : null;

            return planInfo;
        }
        #endregion

        #region # 生产物料需求映射 —— static ProductionMaterialAskInfo ToDTO(this ProductionMaterialAsk...
        /// <summary>
        /// 生产物料需求映射
        /// </summary>
        public static ProductionMaterialAskInfo ToDTO(this ProductionMaterialAsk materialAsk, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, MaterialLotInfo> materialLotInfos)
        {
            ProductionMaterialAskInfo materialAskInfo = materialAsk.Map<ProductionMaterialAsk, ProductionMaterialAskInfo>();
            materialAskInfo.MaterialInfo = materialInfos?[materialAsk.MaterialId];
            materialAskInfo.MaterialLotInfo = materialAsk.MaterialLotId.HasValue
                ? materialLotInfos?[materialAsk.MaterialLotId.Value]
                : null;

            return materialAskInfo;
        }
        #endregion

        #region # 作业计划映射 —— static ExecutionPlanInfo ToDTO(this ExecutionPlan...
        /// <summary>
        /// 作业计划映射
        /// </summary>
        public static ExecutionPlanInfo ToDTO(this ExecutionPlan executionPlan, IDictionary<Guid, MaterialInfo> productInfos, IDictionary<Guid, ProcessInfo> processInfos, IDictionary<Guid, BomInfo> bomInfos, IDictionary<Guid, ProductionPlanInfo> productionPlanInfos, IDictionary<Guid, FacilityInfo> facilityInfos)
        {
            ExecutionPlanInfo planInfo = executionPlan.Map<ExecutionPlan, ExecutionPlanInfo>();
            planInfo.ProductInfo = productInfos?[executionPlan.ProductId];
            planInfo.ProcessInfo = processInfos?[executionPlan.ProcessId];
            planInfo.BomInfo = bomInfos?[executionPlan.BomId];
            planInfo.ProductionPlanInfo = executionPlan.ProductionPlanId.HasValue
                ? productionPlanInfos?[executionPlan.ProductionPlanId.Value]
                : null;
            planInfo.FacilityInfo = executionPlan.FacilityId.HasValue
                ? facilityInfos?[executionPlan.FacilityId.Value]
                : null;

            return planInfo;
        }
        #endregion

        #region # 作业物料需求映射 —— static ExecutionMaterialAskInfo ToDTO(this ExecutionMaterialAsk...
        /// <summary>
        /// 作业物料需求映射
        /// </summary>
        public static ExecutionMaterialAskInfo ToDTO(this ExecutionMaterialAsk materialAsk, IDictionary<Guid, MaterialInfo> materialInfos, IDictionary<Guid, MaterialLotInfo> materialLotInfos)
        {
            ExecutionMaterialAskInfo materialAskInfo = materialAsk.Map<ExecutionMaterialAsk, ExecutionMaterialAskInfo>();
            materialAskInfo.MaterialInfo = materialInfos?[materialAsk.MaterialId];
            materialAskInfo.MaterialLotInfo = materialAsk.MaterialLotId.HasValue
                ? materialLotInfos?[materialAsk.MaterialLotId.Value]
                : null;

            return materialAskInfo;
        }
        #endregion
    }
}
